package com.huaweicloud.metastudio.picturemodeling;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.http.FormDataFilePart;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.metastudio.v1.MetaStudioClient;
import com.huaweicloud.sdk.metastudio.v1.model.ListPictureModelingJobsRequest;
import com.huaweicloud.sdk.metastudio.v1.model.ListPictureModelingJobsResponse;
import com.huaweicloud.sdk.metastudio.v1.model.ListStylesRequest;
import com.huaweicloud.sdk.metastudio.v1.model.ListStylesResponse;
import com.huaweicloud.sdk.metastudio.v1.model.CreatePictureModelingJobRequest;
import com.huaweicloud.sdk.metastudio.v1.model.CreatePictureModelingJobResponse;
import com.huaweicloud.sdk.metastudio.v1.model.CreatePictureModelingJobRequestBody;
import com.huaweicloud.sdk.metastudio.v1.model.ShowPictureModelingJobRequest;
import com.huaweicloud.sdk.metastudio.v1.model.ShowPictureModelingJobResponse;
import com.huaweicloud.sdk.metastudio.v1.region.MetaStudioRegion;

import java.io.FileInputStream;
import java.io.IOException;

public class MetaStudioPictureModeling {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Start HUAWEI CLOUD MetaStudio Picture Modeling Java Demo...");

        ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID");

        // 初始数字人服务的客户端
        MetaStudioClient client = getClient(MetaStudioRegion.CN_NORTH_4, auth);

        // 获取数字人建模任务列表
        listPictureModelingJob(client);

        // 获取数字人建模用的风格
        ListStylesResponse styleResponse = listStyles(client);

        // 风格化数字人建模
        CreatePictureModelingJobResponse createPictureModelingJobResponse = createPictureModelingRequestJob(client,styleResponse.getStyles().get(0).getStyleId());
        Thread.sleep(5000);

        // 查询风格化数字人建模详细
        showPictureModelingJobRequest(client,createPictureModelingJobResponse.getJobId());



    }

    /**
     * 查询查询照片建模任务列表
     *
     * @param client MetaStudio client
     * @return 建模任务查询响应
     */
    public static ListPictureModelingJobsResponse listPictureModelingJob(MetaStudioClient client) {
        System.out.println("listPictureModelingJob start");
        try {
            ListPictureModelingJobsRequest listPictureModelingJobsRequest = new ListPictureModelingJobsRequest();
            ListPictureModelingJobsResponse response = client.listPictureModelingJobs(listPictureModelingJobsRequest);
            System.out.println(response);
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 查询数字人风格
     *
     * @param client MetaStudio client
     * @return 数字人风格查询响应
     */
    public static ListStylesResponse listStyles(MetaStudioClient client) {
        System.out.println("listStyles start");
        try {
            ListStylesRequest listStylesRequest = new ListStylesRequest();
            ListStylesResponse response = client.listStyles(listStylesRequest);
            System.out.println(response);
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 创建风格化照片建模任务
     *
     * @param client MetaStudio client
     * @return 风格化照片建模任务响应
     */
    public static CreatePictureModelingJobResponse createPictureModelingRequestJob(MetaStudioClient client, String style_id) {
        System.out.println("createPictureModelingRequestJob start");
        CreatePictureModelingJobRequestBody createPictureModelingJobRequestBody = new CreatePictureModelingJobRequestBody();
        createPictureModelingJobRequestBody.setName("my_digital_model");
        createPictureModelingJobRequestBody.setStyleId(style_id);
        try(FileInputStream inputStream = new FileInputStream("./assets/picture.jpg")) {
            FormDataFilePart picFile = new FormDataFilePart(inputStream,"picture.jpg");
            createPictureModelingJobRequestBody.setFile(picFile);
            CreatePictureModelingJobRequest createPictureModelingJobRequest = new CreatePictureModelingJobRequest()
                    .withBody(createPictureModelingJobRequestBody);
            CreatePictureModelingJobResponse response = client.createPictureModelingJob(createPictureModelingJobRequest);
            System.out.println(response);
            return response;
        } catch (ConnectionException | RequestTimeoutException | IOException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 查询风格化照片建模详情
     *
     * @param client MetaStudio client
     * @param jobId 风格化照片建模任务ID
     * @return 风格化照片建模任务详情
     */
    public static ShowPictureModelingJobResponse showPictureModelingJobRequest(MetaStudioClient client, String jobId) {
        ShowPictureModelingJobResponse response = null;
        try {
            System.out.println("showPictureModelingJobRequest start");
            ShowPictureModelingJobRequest showPictureModelingJobRequest = new ShowPictureModelingJobRequest();
            showPictureModelingJobRequest.withJobId(jobId);
            response = client.showPictureModelingJob(showPictureModelingJobRequest);
            System.out.println("ShowPictureModelingJobResponse:" + response.toString());
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 创建鉴权凭证
     *
     * @param ak 租户帐号对应的 Access Key（AK）
     * @param sk 租户帐号对应的 Secret Access Key（SK）
     * @return 鉴权凭证
     */
    public static ICredential getCredential(String ak, String sk, String projectId) {
        return new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
    }

    /**
     * 创建MetaStudio client
     *
     * @param region region信息
     * @param auth 鉴权凭证
     * @return MetaStudio client
     */
    public static MetaStudioClient getClient(Region region, ICredential auth) {
        // 使用默认配置
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // 初始化metaStudio服务的客户端
        return MetaStudioClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withRegion(region) // 选择服务所在区域
                .build();
    }
}
